package ltd.followkba.page;

import android.app.DatePickerDialog;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;


import com.kcbsdk.EventHandler;
import com.kcbsdk.MyWorks.APhotoInfo;
import com.kcbsdk.MyManage.AUserFileType;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.BasePage;

import ltd.advskin.base.KcListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.task.TaskFree;
import ltd.advskin.utils.SysBitmap;
import ltd.advskin.utils.SysUtils;
import ltd.advskin.view.KcBtnImage;
import ltd.advskin.view.KcBtnProgress;
import ltd.advskin.view.KcBtnText;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.task.App32;
import ltd.followkba.task.App33;
import ltd.followkba.task.BAppUser;
import ltd.followkba.task.BAppWorks;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.task.EditUtils;
import ltd.followkba.task.UploadApp33;
import ltd.followkba.view.CloneView;
import ltd.obbphoto.MOBBW;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.MSLEEP;
import static main.MAPI.mMyUtils;
import static main.MAPI.mMyWorks;

public class ModifyUserPage extends BasePage {
    private String gsResult;
    private BAppWorks mBAppWorks;
    private BDbsAccount mCloneAccount;
    private BAppUser mModify;
    private BAppUser mOwnerUser;
    private BAppUser mCloneUser;
    private String gsSelectHead;
    private int gSelect;
    private int gUploadQty;
    private int gAreaCode;
    private String gsProvince;
    private String gsCity;

    private KcBtnImage kbiHeadModify;
    private KcBtnText kbtViewModify;
    private View tvInfoModify;
    private EditText etModify0;
    private EditText etModify1;
    private EditText etModify2;
    private EditText etModify3;
    private EditText etModify4;
    private KcBtnProgress kbrChangPhoto;
    private KcBtnProgress kbrUpload;
    private KcBtnProgress kbrSaveModify;
    private TextView tvUserID;
    private RadioButton rbModify0;
    private RadioButton rbModify1;
    private CloneView cvModify;


    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_modify_user);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvModify);
        String text = String.format("修改%s用户资料",  MAPP.mWorksName[MAPP.mBDbsAccount.WorksType]);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;

                }
            }
        });
        kbiHeadModify = findViewById(R.id.kbiHeadModify);
        kbtViewModify = findViewById(R.id.kbtViewModify);
        tvInfoModify = findViewById(R.id.tvInfoModify);
        etModify0 = findViewById(R.id.etModify0);
        etModify1 = findViewById(R.id.etModify1);
        etModify2 = findViewById(R.id.etModify2);
        etModify3 = findViewById(R.id.etModify3);
        etModify4 = findViewById(R.id.etModify4);
        kbrChangPhoto = findViewById(R.id.kbrChangPhoto);
        kbrSaveModify = findViewById(R.id.kbrSaveModify);
        tvUserID = findViewById(R.id.tvUserID);
        rbModify0 = findViewById(R.id.rbModify0);
        rbModify1 = findViewById(R.id.rbModify1);
        kbrUpload = findViewById(R.id.kbrUpload);

        etModify2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 生日
                showBirthday();
            }
        });
        etModify3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 输入地区
                MOBBW.startAreaCityRankWindow(false, new KcListener() {
                    @Override
                    public void onMessage(Object object){
                        gAreaCode = (int) object;
                        gsProvince = mMyUtils.getAreaDataProvince(gAreaCode);
                        gsCity = mMyUtils.getAreaDataCity(gAreaCode);
                        showInfo();
                    }
                });
            }
        });

        kbrSaveModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 更改资料
                updateInfo();
            }
        });
        kbrSaveModify.setProgress(2);
        kbtViewModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 查看
                MAPP.startPhotoWorksPage(getSelect());
            }
        });

        kbiHeadModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 头像
                MOBBW.startPhotoLookAtActivity(gsSelectHead, v);
            }
        });
        kbrChangPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 换图
                MAPP.startLocalPhotoPage();
            }
        });
        kbrUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 上传
                uploadPhoto();
            }
        });

        rbModify0.setChecked(true);
        rbModify0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 选择自己
                selectUser(0);
            }
        });
        rbModify1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 克隆账号
                selectUser(1);
            }
        });

        cvModify = findViewById(R.id.cvModify);
        cvModify.setListener(new KcListener() {
            @Override
            public void onMessage(Object object) {
                switch ((int)object){
                    case 1:
                        BAppUser bAppUser = MDBS.getBAppUser(mCloneAccount.CloneAppType, mCloneAccount.CloneUserIdS);
                        if (bAppUser == null){
                            bAppUser = new BAppUser();
                            bAppUser.WorksType = mCloneAccount.CloneAppType;
                            bAppUser.UserIdS = mCloneAccount.CloneUserIdS;
                        }
                        MAPP.startUserHomePage(bAppUser, false);
                        break;
                    case 2:                                 // 保存克隆
                        MAPP.mBDbsAccount.CloneUserIdS = mCloneAccount.CloneUserIdS;
                        MAPP.mBDbsAccount.CloneUserName = mCloneAccount.CloneUserName;
                        MAPP.mBDbsAccount.CloneHead = mCloneAccount.CloneHead;
                        MAPP.mBDbsAccount.CloneAppType = mCloneAccount.CloneAppType;
                        MDBS.setBDbsAccount(MAPP.mBDbsAccount);
                        MTOAST("已经保存克隆", 2000);
                        break;
                    case 3: MAPP.startSelectUserPage(false); break;   // 其他用户
                }
            }
        });

        mCloneAccount = new BDbsAccount();
        mCloneAccount.CloneHead = MAPP.mBDbsAccount.CloneHead;
        mCloneAccount.CloneUserIdS = MAPP.mBDbsAccount.CloneUserIdS;
        mCloneAccount.CloneUserName = MAPP.mBDbsAccount.CloneUserName;
//        mCloneSelect.CloneAppName = MAPP.mBDbsAccount.CloneAppName;
        if (!MAPI.isEmpty(MAPP.mBDbsAccount.CloneUserIdS)) {
            mCloneAccount.CloneAppType = MAPP.mBDbsAccount.CloneAppType;
        }else {
            mCloneAccount.CloneAppType = MAPP.mBDbsAccount.WorksType;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int index = 0; index < MAPP.mBDbsAccount.SourceText.length; index++){
            stringBuffer.append(MAPP.mBDbsAccount.SourceText[index]);
            stringBuffer.append(",");
        }
        MSKIN.setText(stringBuffer.toString(), findViewById(R.id.tvSrcModify));
        MTOAST("重新读取用户信息，请稍候...", 5000);
    }


    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
            long tick = System.currentTimeMillis();
            if (MAPP.mBDbsAccount.WorksType == 33 || MAPP.mBDbsAccount.WorksType == 49 || MAPP.mBDbsAccount.CloneAppType == 33 || MAPP.mBDbsAccount.CloneAppType == 49){
                App33.setUser(null);                                        // App33需要一个默认的账号
            }
            if (MAPP.mBDbsAccount.WorksType < 32 || MAPP.mBDbsAccount.CloneAppType < 32){
                App32.setUser(MAPP.mBDbsAccount);
            }
            EditUtils editUtils = new EditUtils();
            mOwnerUser = editUtils.getUserInfo(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS, AUserFileType.IMG_LARGE);
            if (mOwnerUser != null) {
                MAPP.mBDbsAccount.HeadFile = mOwnerUser.HeadFile;
                MDBS.setBDbsAccount(MAPP.mBDbsAccount);
                String filePath = MDBS.getUserPath(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
                MAPI.DELETE_FILE(filePath);
                MDBS.setBAppUser(mOwnerUser);
            }
            getCloneUser();
//            if (mOwnerUser!=null)MLOG(String.format("UserInfoA mOwnerUser %s %s", MAPI.GET_FILE_LENGTH(mOwnerUser.HeadFile), mOwnerUser.HeadFile));
//            if (mCloneUser!=null)MLOG(String.format("UserInfoA mCloneUser %s %s", MAPI.GET_FILE_LENGTH(mCloneUser.HeadFile), mCloneUser.HeadFile));
            mBAppWorks = getPhotoAlbum(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
            MLOG("UserInfoA long tick = System.currentTimeMillis(); "+(System.currentTimeMillis()-tick));

        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
                        showInfo();
                    }
                }
            }
        });
        showInfo();
        kbrSaveModify.setProgress(2 + 100);

    }
    @Override
    public void onPageUpdate(BasePage basePage){
        Object object = getPageResult(basePage);
        if (object instanceof BAppUser) {
            BAppUser bAppUser = (BAppUser)object;
            mCloneUser = bAppUser;
            mCloneAccount.CloneHead = mCloneUser.HeadFile;
            mCloneAccount.CloneUserIdS = mCloneUser.UserIdS;
            mCloneAccount.CloneUserName = mCloneUser.UserName;
            mCloneAccount.CloneAppType = mCloneUser.WorksType;

            if (bAppUser != null && basePage instanceof SelectUserPage) {       // 自动保存
                MAPP.mBDbsAccount.CloneUserIdS = mCloneAccount.CloneUserIdS;
                MAPP.mBDbsAccount.CloneUserName = mCloneAccount.CloneUserName;
                MAPP.mBDbsAccount.CloneHead = mCloneAccount.CloneHead;
                MAPP.mBDbsAccount.CloneAppType = mCloneAccount.CloneAppType;
                MDBS.setBDbsAccount(MAPP.mBDbsAccount);
                MTOAST("已经保存克隆", 2000);
            }

//            if (bAppUser != null && mCloneUser != null && !bAppUser.UserIdS.equals(mCloneUser.UserIdS)){
                new TaskFree(bAppUser, new TaskFree.Listener() {
                    @Override
                    public void onMessage(boolean isThread, Object object) {
                        if (isThread){
                            BAppUser bAppUser = (BAppUser)object;
                            downloadHeadFile(bAppUser.WorksType, bAppUser);

                            getCloneUser();
                            return;
                        }
                        showInfo();
                    }
                });
//
//            }
        }
        onInitData();
    }

    private void showInfo(){
        BAppUser bAppUser = getSelect();
        String fileName;
        if (bAppUser != null){
            fileName = bAppUser.HeadFile;
            MAPP.showUserHead(bAppUser.HeadFile, kbiHeadModify);
            MSKIN.setText(bAppUser.UserName, etModify0);
            MSKIN.setText(String.format("%s", bAppUser.Gender==0?"女":"男"), etModify1);
            if (bAppUser.WorksType < 32) {
                MSKIN.setText(String.format("KCID:%s", bAppUser.UserIdS), tvUserID);
            }else {
                MSKIN.setText(String.format("ID:%s", bAppUser.UserIdS), tvUserID);
            }
            if (!MAPI.isEmpty(bAppUser.Birthday)) {
                MSKIN.setText(bAppUser.Birthday, etModify2);
            }
            if (gsProvince != null && gsCity != null) {                       // 自行选择了省市
                MSKIN.setText(String.format("%s %s ", gsProvince, gsCity), etModify3);
            }else {
                if (!MAPI.isEmpty(bAppUser.Province) && !MAPI.isEmpty(bAppUser.City)) {               // 省市都有
                    MSKIN.setText(String.format("%s %s ", bAppUser.Province, bAppUser.City), etModify3);
                } else if (!MAPI.isEmpty(bAppUser.City)) {                                        // 否则市有
                    MSKIN.setText(String.format("%s", bAppUser.City), etModify3);
                } else if (!MAPI.isEmpty(bAppUser.Province)) {                                    // 否则省有
                    MSKIN.setText(String.format("%s", bAppUser.Province), etModify3);
                }
            }
            if (!MAPI.isEmpty(bAppUser.Intro) && !" ".equals(bAppUser.Intro)) {     // 有些用户使用的介绍为空格
                MSKIN.setText(bAppUser.Intro, etModify4);
            }
        }else {
            fileName = gsSelectHead;
            MAPP.showUserHead(gsSelectHead, kbiHeadModify);
            MSKIN.setText(MAPP.mBDbsAccount.UserName, etModify0);
            MSKIN.setText(String.format("ID:%s", MAPP.mBDbsAccount.UserIdS), tvUserID);
        }
        MAPP.showUserHead(fileName, kbiHeadModify);
        SysBitmap sysBitmap = new SysBitmap();
        int g4Local_1 = sysBitmap.getBitmapSize(fileName);
        int width = g4Local_1 >> 16;
        int height = g4Local_1 & 0xffff;
        double fileSize = (double) MAPI.GET_FILE_LENGTH(fileName) / 1024.0;
        DecimalFormat df2 = new DecimalFormat("0.00 ");
        String text = String.format("%sx%s(%sK)", width, height, df2.format(fileSize));
        MSKIN.setText(text, tvInfoModify);
        if (mBAppWorks != null && mBAppWorks.Photos != null){
            MSKIN.setText(String.format("查看(%s)", mBAppWorks.Photos.length), kbtViewModify);
        }else {
            MSKIN.setText(String.format("查看"), kbtViewModify);
        }
        cvModify.setInfo(mCloneAccount);
    }
    private void getCloneUser(){
        EditUtils editUtils = new EditUtils();
        mCloneUser = editUtils.getUserInfo(MAPP.mBDbsAccount.CloneAppType, MAPP.mBDbsAccount.CloneUserIdS, AUserFileType.IMG_LARGE);
//            MLOG(String.format("UserInfoA c %s(%s)%s", MAPP.mBDbsAccount.CloneAppType, MAPP.mBDbsAccount.CloneUserIdS, mCloneUser));
        if (mCloneUser != null) {
            String filePath = MDBS.getUserPath(MAPP.mBDbsAccount.CloneAppType, MAPP.mBDbsAccount.CloneUserIdS);
            MAPI.DELETE_FILE(filePath);
            MDBS.setBAppUser(mCloneUser);
        }
    }


    private void selectUser(int index){                     // 选择自己 克隆账号
        if (gSelect != index) {
            gsSelectHead = null;
            if (index == 0){
                gSelect = 0;
                MTOAST(String.format("已选择选择自己", 2000));
            }else if (index == 1) {
                if (mCloneUser != null) {
                    gSelect = 1;
                    MTOAST(String.format("已选择克隆账号", 2000));
                }else {
                    MTOAST(String.format("还没有选择克隆账号", 2000));
                    return;
                }
            }
            new TaskFree(null, new TaskFree.Listener() {
                @Override
                public void onMessage(boolean isThread, Object object) {
                    if (isThread){
                        BAppUser bAppUser = getSelect();
                        if (bAppUser != null) {
                            mBAppWorks = getPhotoAlbum(bAppUser.WorksType, bAppUser.UserIdS);
                            gsSelectHead = bAppUser.HeadFile;
                            MLOG(String.format("UserInfoA HeadFileA B %s %s", MAPI.GET_FILE_LENGTH(bAppUser.HeadFile), bAppUser.HeadFile));
                        }
                        return;
                    }
                    showInfo();
                }
            });
        }
    }

    private BAppUser getSelect(){
        return (gSelect == 0) ? mOwnerUser : mCloneUser;
    }

    private void downloadHeadFile(int appType, BAppUser bAppUser){
        if (appType < 32) {
            bAppUser.HeadFile = App32.downloadHeadFile(AUserFileType.IMG_LARGE, bAppUser, true);
        }else if (appType == 33 || appType == 49) {
            bAppUser.HeadFile = App33.downloadHeadFile(AUserFileType.IMG_LARGE, bAppUser, true);

        }else if (appType == 39){
            bAppUser.HeadFile = String.format("%sH28_%s.jpg", MRAM.gsCachePath, bAppUser.UserIdS);
            EditUtils utils = new EditUtils();
            utils.downloadPhoto(false, 0, 0, bAppUser.HeadUrl, bAppUser.HeadFile);
        }
        MLOG(String.format("UserInfoA HeadFileA A %s %s", MAPI.GET_FILE_LENGTH(bAppUser.HeadFile), bAppUser.HeadFile));
        MDBS.setBAppUser(bAppUser);
    }

    private void updateInfo() {
        gsResult = null;
        mModify = new BAppUser();
        mModify.UserName = etModify0.getText().toString();
        if (MAPI.isEmpty(mModify.UserName)){
            MTOAST("请输入正确的昵称", 2000);
            return;
        }
        String text = etModify1.getText().toString();
        if (MAPI.isEmpty(mModify.UserName) || (!"女".equals(text) && !"男".equals(text))){
            MTOAST("请输入正确的性别", 2000);
            return;
        }
        mModify.Gender = ("女".equals(text) ? 0 : 1);
        text = etModify2.getText().toString();
        long dateTime = makeBirthday(text);
        if (dateTime == 0){
            MTOAST("请输入正确的生日(格式：年年年年-月月-日日)", 2000);
            return;
        }
        mModify.Birthday = text;

        text = etModify3.getText().toString();
        String[] split = text.split(" ");
        if (split.length < 1 || "null".equals(split[0])){
            MTOAST("请输入正确的地区(格式：省 市)", 2000);
            return;
        }
        if (split.length > 1){
            mModify.Province = split[0];
            mModify.City = split[1];
        }else {
            mModify.City = split[0];
        }

        kbrSaveModify.setProgress(2);
        mModify.Intro = etModify4.getText().toString();
        if (MAPI.isEmpty(mModify.Intro)){
            MTOAST("请输入正确的签名", 2000);
            return;
        }

        new TaskFree(null, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                if (isThread){
                    EditUtils editUtils = new EditUtils();
                    if (editUtils.waitWriteLock("修复_信息")) {
                        if (MAPP.mBDbsAccount.WorksType < 32) {
                            App32.setUser(MAPP.mBDbsAccount);
                            gsResult = App32.modifyUseInfo(mModify);
                            MLOG("UserInfoA String result "+ gsResult);

                        }else if (MAPP.mBDbsAccount.WorksType == 33 || MAPP.mBDbsAccount.WorksType == 49) {
                            gsResult = App33.modifyUseInfo(1, mModify);
                            MLOG("UserInfoA String result " + gsResult);
                        }
                        editUtils.clearWriteLock();
                    }
                    return;
                }
                kbrSaveModify.setProgress(102);
                if ("ok".equals(gsResult)){
                    showInfo();
                    EditUtils editUtils = new EditUtils();
                    editUtils.getAndSaveBDbsAccountInfo(new KcListener() {
                        @Override
                        public void onMessage(Object object) {
                            if ((boolean)object){
                                mOwnerUser = MDBS.getBAppUser(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
                                mBAppWorks = getPhotoAlbum(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
                                return;
                            }
                            MTOAST("更改资料成功，自动关闭页面...", 3000);
                            setClose();
                        }
                    });
                }else {
                    MTOAST(String.format("更改资料出错：%s", gsResult), 2000);
                }
            }
        });
    }


    private void uploadPhoto(){
//        MLOG(String.format("上传图片 A1 %s %s", MAPI.GET_FILE_LENGTH(gsUserHead), gsUserHead));
        SysBitmap sysBitmap = new SysBitmap();
        int g4Local_1 = sysBitmap.getBitmapSize(gsSelectHead);
        int width = g4Local_1 >> 16;
        int height = g4Local_1 & 0xffff;
        if (width < 320 || height < 320){
            MTOAST(String.format("上传失败，图片的像素太低了%sx%s(%s)%s", width, height, MAPI.GET_FILE_LENGTH(gsSelectHead), gsSelectHead), 2000);
            return;
        }
        kbrUpload.setProgress(2);
        BAppUser bAppUser = getSelect();
        if (bAppUser != null) {
            gsResult = null;
            gUploadQty = 0;
            new TaskFree(bAppUser, new TaskFree.Listener() {
                @Override
                public void onMessage(boolean isThread, Object object) {
                    if (isThread) {
//                        BAppUser bAppUser = (BAppUser)object;
                        BAppWorks bPhotoWorks = new BAppWorks();
                        bPhotoWorks.HeadFile = gsSelectHead;
                        if (MAPP.mBDbsAccount.WorksType == 33) {
                            UploadApp33 uploadApp33 = new UploadApp33(MAPP.mBDbsAccount.LoginFlag, MAPP.mBDbsAccount.WorksType);
                            uploadApp33.setInfo(mCloneAccount.CloneUserIdS, MAPP.mBDbsAccount, "上传图片");
                            if (bPhotoWorks.Photos != null) {
                                SysUtils sysUtils = new SysUtils();
                                for (int index = 0; index < bPhotoWorks.Photos.length; index++) {
                                    final double progress = ((double) index / (double)bPhotoWorks.Photos.length) * 100.0;
                                    MRAM.mActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            kbrUpload.setProgress(2 + (int)progress);
                                        }
                                    });
                                    APhotoInfo aPhotoInfo = bPhotoWorks.Photos[index];
                                    aPhotoInfo.FileName = String.format("%sphoto/P%02x_%s.jpg", MRAM.gsCachePath, MAPP.mBDbsAccount.WorksType, sysUtils.getStringMd5(aPhotoInfo.FileUrl));
                                    gsResult = sysUtils.getUrlFile(aPhotoInfo.FileUrl, aPhotoInfo.FileName);
                                    if ("error,4".equals(gsResult)){
                                        gsResult = sysUtils.getUrlFile(aPhotoInfo.FileUrl.replace(".jpg", "_640_640.jpg"), aPhotoInfo.FileName);
                                    }
                                    if (MAPI.GET_FILE_LENGTH(aPhotoInfo.FileName) > 500){
                                        gsResult = uploadApp33.uploadPhoto(aPhotoInfo.FileName);
                                        MLOG(String.format("上传%s %s %s", index, MAPI.GET_FILE_LENGTH(aPhotoInfo.FileName), aPhotoInfo.FileName));
                                        ++gUploadQty;
                                    }else {
                                        gsResult = "下载图片出错";
                                    }
                                }
                            }else {             // 单图
                                gsResult = uploadApp33.uploadPhoto(bPhotoWorks.HeadFile);
                                gUploadQty = 1;
//                                    gsResult = "没有需要上传的图片";
                            }
                        }else if (MAPP.mBDbsAccount.WorksType < 32) {
                            SysBitmap sysBitmap = new SysBitmap();
                            int g4Local_1 = sysBitmap.getBitmapSize(bPhotoWorks.HeadFile);
                            if (g4Local_1 > 0) {
                                int width = g4Local_1 >> 16;
                                int height = g4Local_1 & 0xffff;
                                int reqWidth = 1024;
                                int reqHeight = 1024;
                                String fileName = null;
                                if (width == height){                                                       // 正方形的图片
                                    reqWidth = width;
                                    reqHeight = height;
                                    if (MAPI.GET_FILE_LENGTH(bPhotoWorks.HeadFile) < 200000) {               // 正方形的图片而且小于200KB的
                                        fileName = bPhotoWorks.HeadFile;
                                    }
                                }
                                if (fileName == null){                                                      // 需要修整图片
                                    Bitmap bitmap = sysBitmap.getReqBitmap(reqWidth, reqHeight, 0, 0, 0, 0, 0, bPhotoWorks.HeadFile);
                                    MLOG(String.format("上传图片 A %s %s %s %s", width, height, MAPI.GET_FILE_LENGTH(bPhotoWorks.HeadFile), bPhotoWorks.HeadFile));
                                    if (bitmap != null) {
                                        int quality = 60;
                                        fileName = String.format("%sphoto/App32.jpg", MRAM.gsCachePath);
                                        if (sysBitmap.saveBitmapFile(bitmap, fileName, quality)) {
                                        } else {
                                            fileName = null;
                                        }
                                        bitmap.recycle();
                                    }
                                }
                                if (fileName != null){
                                    EditUtils editUtils = new EditUtils();
                                    if (editUtils.waitWriteLock("上传_图片")) {
                                        App32.setUser(MAPP.mBDbsAccount);
                                        gsResult = App32.uploadPhotoFile(fileName, AUserFileType.IMG_LARGE);
                                        editUtils.clearWriteLock();
                                    }
                                    MLOG(String.format("上传图片 B %s %s", MAPI.GET_FILE_LENGTH(fileName), gsResult));
                                    if ("ok".equals(gsResult)) {
                                        String large = mMyUtils.getUserFileName(AUserFileType.IMG_LARGE, MAPP.mBDbsAccount, MRAM.gsCachePath);
                                        String middle = mMyUtils.getUserFileName(AUserFileType.IMG_MIDDLE, MAPP.mBDbsAccount, MRAM.gsCachePath);
                                        String small = mMyUtils.getUserFileName(AUserFileType.IMG_SMALL, MAPP.mBDbsAccount, MRAM.gsCachePath);
                                        MAPI.COPY_FILE(fileName, large);
                                        MAPI.SET_FILE_DONE(large);
                                        MAPI.COPY_FILE(fileName, middle);
                                        MAPI.SET_FILE_DONE(middle);
                                        MAPI.COPY_FILE(fileName, small);
                                        MAPI.SET_FILE_DONE(small);
                                        MAPI.COPY_FILE(fileName, MAPP.mBDbsAccount.HeadFile);
                                        MAPI.SET_FILE_DONE(MAPP.mBDbsAccount.HeadFile);
                                        MLOG(String.format("MAPP.mBDbsAccount.HeadFile B %s %s", MAPI.GET_FILE_LENGTH(MAPP.mBDbsAccount.HeadFile), MAPP.mBDbsAccount.HeadFile));
                                    }
                                }
                            }
                            gUploadQty = 1;
                            if (gsResult == null){
                                gsResult = "图片文件不正确";
                            }
                        }
                        return;
                    }
                    kbrUpload.setProgress(102);
                    if ("ok".equals(gsResult)){
                        EditUtils editUtils = new EditUtils();
                        editUtils.getAndSaveBDbsAccountInfo(new KcListener() {
                            @Override
                            public void onMessage(Object object) {
                                if ((boolean)object){
                                    mOwnerUser = MDBS.getBAppUser(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
                                    mBAppWorks = getPhotoAlbum(MAPP.mBDbsAccount.WorksType, MAPP.mBDbsAccount.UserIdS);
                                    return;
                                }
//                                gSelect = 0;
//                                rbModify0.setChecked(true);
//                                showInfo();
                                MTOAST(String.format("上传%s张图片成功，自动关闭页面...", gUploadQty), 3000);
                                setClose();
                            }
                        });

                    }else {
                        MTOAST(String.format("上传图片出错：%s", gsResult), 2000);
                    }
                }
            });
        }else {
            MTOAST(String.format("用户资料出错"), 2000);
        }
    }

    private BAppWorks getPhotoAlbum(int worksType, String userIdS){
        if (worksType == 33 || worksType == 49) {
//            App33 app33 = MAPP.mAccount33;
//            if (app33 == null) {
//                app33 = new App33(null);
//            }
            return App33.getPhotoAlbum(userIdS);
        }
        return null;
    }
    public void showBirthday() {
        String text = etModify2.getText().toString();
        if (MAPI.isEmpty(text)){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            text = String.format("%04d-%02d-%02d", calendar.get(Calendar.YEAR) - 18, calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        }
        long dateTime = makeBirthday(text);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);        // 创建一个日历引用d，通过静态方法getInstance() 从指定时区 Locale.CHINA 获得一个日期实例
        calendar.setTime(new Date(dateTime));
        MLOG("showBirthday C "+calendar.get(Calendar.YEAR));
        int themeresid = 2;                             // themeresid表示的是效果的背景色，设置为2就是上面的效果图
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), themeresid, new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String input = String.format("%04d-%02d-%02d", year, monthOfYear+1, dayOfMonth);
                MSKIN.setText(input, etModify2);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setMessage("请选择日期");
        datePickerDialog.show();
    }

    private long makeBirthday(String input){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(input);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

}

